<?php
/**
 * JE Career package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2012 - 2013 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view' );
jimport('joomla.utilities.date');
jimport('joomla.html.pagination');

class  hostingrajacareerViewCategory  extends JView
{
	function display($tpl = null)
	{
		if( $this->_layout == 'singlecategory') {
			$this->_categorylistLayout( $tpl );
			return;

		} else {
			$this->_defaultLayout( $tpl );
			return;
		}
	}

	function _defaultLayout( $tpl )
	{
		global $mainframe;

		$catid				= '';
		$db 				= & JFactory::getDBO();
		$user				= & JFactory::getUser();
		$model  			= & $this->getModel();
		$settings			= $model->getGlobalsettings();
		$limit				= $settings->cat_perpage;
		$limitstart 		= JRequest::getVar('limitstart', 0, '', 'int');
   		$limitstart 		= ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		$catid				= $model->getAllCareers();
		$where 				= array();
		$where[] 			= 's.state = 1';   // while published
		$where[] 			= 's.id IN ('.$catid.')';

		// From Search Career's using default search module.
			$sid			= & JRequest::getVar('sid');
			if ( is_numeric ( $sid ) ) {
				$where[]	= 's.id='. $db->Quote( $sid );
			}
		$filter_order		= 's.category, s.'.$settings->group.' '.$settings->sorting;
		$where				= count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
		$orderby			= ' ORDER BY '. $filter_order .', s.id';

		// get the total number of records
		$query 				= 'SELECT count(*) FROM #__hostingraja_career_category AS s '.
								$where
								;
		$db->setQuery( $query );
		if (!$db->query())	{
			echo $db->getErrorMsg();
		}

		$total 				= $db->loadResult();
		$pageNav   			= new JPagination( $total, $limitstart, $limit );
		$query				= 'SELECT s.* FROM #__hostingraja_career_category AS s '
							  	. $where
								. $orderby
								;
		$db->setQuery( $query, $limitstart, $limit );
		if (!$db->query())	{
			echo $db->getErrorMsg();
		}
		$rows 				= $db->loadObjectList();

		// Get title..
		$params				= & $mainframe->getParams('com_content');
		$this->assignRef('items',		$rows);
		$this->assignRef('params',		$params);
		$this->assignRef('settings',	$settings);
		$this->assignRef('user',		$user);
		$this->assignRef('pageNav',		$pageNav);

		parent::display($tpl);
	}

	function _categorylistLayout( $tpl )
	{
		global $mainframe;

		$career->catid			= JRequest::getInt( 'catid',0);
		$cid 				= $career->catid;
		$catid				= '';
		$db 				= & JFactory::getDBO();
		$user				= & JFactory::getUser();
		$model  			= & $this->getModel();
		$settings			= $model->getGlobalsettings();
		$limit				= $settings->cat_perpage;
		$limitstart 		= JRequest::getVar('limitstart', 0, '', 'int');
   		$limitstart 		= ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		$catid				= $model->getAllCareers();
		$where 				= array();

		if($cid){
			if (!$db->query())	{
				echo $db->getErrorMsg();
			}
			$query 			= 'SELECT * FROM #__hostingraja_career_category AS s WHERE id='.$cid;
			$db->setQuery( $query );
			$category_des 	= $db->loadObject();
			$this->assignRef('catdes',		$category_des);
			$where[] 		= 's.catid ='.$cid;
		}
		$where[] 			= 's.state = 1';   // while published
		$where[] 			= 's.catid IN ('.$catid.')';

		// From Search Career's using default search module.
			$sid			= & JRequest::getVar('sid');
			if ( is_numeric ( $sid ) ) {
				$where[]	= 's.id='. $db->Quote( $sid );
			}

		$filter_order		= 'c.category, s.'.$settings->group.' '.$settings->sorting;
		$where				= count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
		$orderby			= ' ORDER BY '. $filter_order .', s.id';

		// get the total number of records
		$query 				= 'SELECT count(*) FROM #__hostingraja_career AS s '.
								$where
								;
		$db->setQuery( $query );

		if (!$db->query())	{
			echo $db->getErrorMsg();
		}

		$total 				= $db->loadResult();
		$pageNav   			= new JPagination( $total, $limitstart, $limit );
		$query				= 'SELECT s.* FROM #__hostingraja_career AS s '.
								' LEFT JOIN ' .
						   		' #__hostingraja_career_category AS c ON c.id = s.catid'
							  	. $where
								. $orderby
								;
		$db->setQuery( $query, $limitstart, $limit );

		if (!$db->query())	{
			echo $db->getErrorMsg();
		}
		$rows 				= $db->loadObjectList();

		// Get title..
		$params				= & $mainframe->getParams('com_content');
		$this->assignRef('items',		$rows);
		$this->assignRef('params',		$params);
		$this->assignRef('settings',	$settings);
		$this->assignRef('user',		$user);
		$this->assignRef('pageNav',		$pageNav);

		parent::display($tpl);
	}
}
